<?php

include_once 'D:/dingjiangbbs/common/home.php';

//屏蔽notice错误
error_reporting(E_ALL ^ E_NOTICE);

//获取number 的值
//var_dump($_GET);
$number = is_null($_GET['number'])?null:$_GET['number'];
//var_dump($number);

//查询显示用户原信息
$username = $_COOKIE['username'];
//var_dump($username);
$table    = DB_TABLE_USER;
//var_dump(DB_TABLE_USER);
$fields   = "realname,sex,birthday,place,qq,autograph,picture,password,email";
$where    =  "where username='$username'";	
$res 	  = select($link,$table,$fields,$where);
var_dump($res);

/****************************************************/
/**********修改头像*******************************/
/****************************************************/

if ($number == 1) {
	//用FILES接收值
	//var_dump($_FILES);

	$picture0 = $res[0]['picture'];
	//var_dump($picture0);

	//若图片地址
	if (empty($_FILES['picture']['name'])) {

		exit('未输入文件，当前头像未修改');
		//$picture = $picture0;
	}

	//打印图片文件信息
	$name = $_FILES['picture']['name'];
	$picture = 'views/home/image/'.$name;
	//var_dump($picture);

	//修改数据库字段
	$data = ['picture' => "$picture"];
	$res_tx = update($link,DB_TABLE_USER,$data,"username='$username'");
	//var_dump($res_tx);

	//页面跳转
	echo '修改成功';
	header('refresh:1;url='.WEB_SITE.'set_up.php?number=1');

}
/****************************************************/
/**********修改个性签名*******************************/
/****************************************************/

//将修改后的信息插入数据库bbs_user表
	//获取post值（注意：post传送的值为字符串）
if ($number == 3) {
	$autograph = trim($_POST['autograph']);
	$where2    = "username='$username'";
	$data000   = [
		'autograph' => "$autograph"
	];
	$res = update($link , $table , $data000 , $where2);
	if (!$res) {
		echo  '修改失败';
	}
	//var_dump($res);
	echo '修改成功';
	//header('refresh:1;url='.WEB_SITE.'set_up.php');
} 
/****************************************************/
/**********修改个人资料*******************************/
/****************************************************/

//将修改后的信息插入数据库bbs_user表
if ($number == null || $number == 2) {

		//获取post值（注意：post传送的值为字符串）
		//var_dump($_POST);
		$realname = $_POST['realname'];
		$sex      = $_POST['sex'];
		$sex      = (int)$sex;		//强制类型转换为整形
		//var_dump($sex);
		$birthday = $_POST['birthday'];
		//var_dump($birthday);
		$birthday = strtotime("$birthday");	//将年-月-日格式转化为时间戳格式
		//var_dump($birthday);
		$place = $_POST['place'];
		$qq	   = $_POST['qq'];

		$data  = [
			
			'realname' => "$realname",
			'sex'	   => "$sex",
			'birthday' => "$birthday",
			'place'    => "$place",
			'qq'	   => "$qq"	
		];

		$where2 = "username='$username'";
		//var_dump($data,$table);

		$res    = update($link , $table , $data , $where2);
		//var_dump($res);
		if ($res) {
			
			echo '<a href="'.WEB_SITE.'set_up.php">修改成功,若无跳转请点击此处</a>';
			//header('refresh:2;url='.WEB_SITE.'set_up.php');
		}

	}
	//header('refresh:3;url='.WEB_SITE.'set_up.php');

/****************************************************/
/**********修改个人资料*******************************/
/****************************************************/
	if ($number == 4) {

		var_dump($_POST);

		$resx = select($link,DB_TABLE_USER,'password',"where username='$username'");
		var_dump($resx[0]['password']);

		$pwd = $resx[0]['password'];
		var_dump($pwd);
		$oldPassword   = trim($_POST['oldPassword']);
		$newPassword   = trim($_POST['newPassword']);
		$reNewPassword = trim($_POST['reNewPassword']);
		$email         = trim($_POST['email']);
		$safeProblem   = trim($_POST['safeProblem']);
		$answer        = trim($_POST['answer']);

		//$new_pwd = md5($newPassword);
		//var_dump($new_pwd);
		var_dump(md5(dingjiang));
		var_dump($oldPassword);
		//判断密码是否错误
		/*
		if (md5($oldPassword) != "$pwd") {

			exit('密码输入错误');
		}
		*/
		//判断新密码类型
		if (strlen($newPassword) <3 || strlen($newPassword) > 12) {

			exit('密码应在3~12位之间的字符串');
		}

		if (is_numeric($newPassword)) {

			exit('密码不能为纯数字');
		}

		if ($newPassword !== $reNewPassword) {

			exit('两次新密码输入不一致');
		}

		$new_pwd = md5($newPassword);

		//判断邮箱格式
		$ePattern = "/([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?/i";

		if (!preg_match($ePattern,$email)) {
			exit('邮箱格式错误');
		}

		//判断安全问题
		if ($safeProblem == 'none') {

			$result = '空';
		}

		$data_pwd  = [
			'password' => "$new_pwd",
			'email'    => "$email",
			'email'    => "$email",
			'problem'  => "$problem",
			'result'   => "$result"

		];
		$resy = update($link,DB_TABLE_USER,$data_pwd,"username='$username'");
		var_dump($resy);

	}
	echo '操作成功';
	/*************关闭数据库连接***************************************/
	mysqli_close($link);

	/*************全部结束***************************************/


